package com.lc.user.express.ordering;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.user.express.BaseSecondActivity;
import com.lc.user.express.R;
import com.lc.user.express.adapter.AddrAdapter;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.httpserver.GetAddrAdd;
import com.lc.user.express.httpserver.GetIsBalance;
import com.lc.user.express.httpserver.GetOrderCommit;
import com.lc.user.express.model.AddrModel;
import com.lc.user.express.utils.GetToast;
import com.lc.user.express.utils.PublicMethod;
import com.lc.user.express.view.MyListView;
import com.lc.user.express.view.dialog.InPutDialog;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.yun.YunAction;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/8.订单确认
 */
public class OrderInfoSureActivity extends BaseSecondActivity {
    private ListView lv;//地址列表
    private List<AddrModel> list = new ArrayList<>();
    private List<AddrModel> submitlist = new ArrayList<>();
    private AddrAdapter adapter;
    private TextView tv_time;// 用车时间
    private TextView tv_car_name;//车型
    private TextView tv_price;//预估价格
    private TextView tv_coupon;//减免

    private TextView tv_tip;//加小费
    private TextView tv_tip_money;//加小费具体值
    private String tip = "";// 小费

    private TextView tv_send_pay;//发货时支付选中
    private TextView tv_rec_pay;//收货时支付选中
    private LinearLayout ll_pay_method_container;//选择付费方式
    private RelativeLayout rl_cash_pay;//现金支付
    private RelativeLayout rl_acount_pay;//余额支付
    private TextView tv_acount_pay;//余额支付选中
    private TextView tv_cash_pay;//现金支付选中
    private int payttime;//付款时间： 1 发货时付款 2收货时付款
    private int paytype;//付款方式 ：1余额 2现金支付线下
    private TextView tv_balance_pay;//余额支付描述
    private ImageView iv_alert;//叹号
    private boolean balanceEnough = true;//账户余额是否充足
    private String money;//预估价格

    private GetOrderCommit orderCommit = new GetOrderCommit(AppContext.myPreferences.getCityId(),1, "", "", "", "", "", "", "", "", "", 0, 0, 0, 0, 0, new AsyCallBack<GetOrderCommit.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetOrderCommit.Info info) throws Exception {
            String[] latLng = list.get(0).getPoint().split(",");
            JSONObject object = new JSONObject();
            object.put("cityid", AppContext.myPreferences.getCityId());
            object.put("carid", getIntent().getStringExtra("cid"));
            object.put("jingdu", latLng[1]);
            object.put("weidu", latLng[0]);
            object.put("orderid", info.orderid);
            object.put("type", "1");
            YunAction.getInstance().sendGroupMessage(OrderInfoSureActivity.this, object.toString());
            Bundle bundle = new Bundle();
            bundle.putString("orderid", info.orderid);
            bundle.putString("spoint", list.get(0).getPoint());
            bundle.putString("start_addr", list.get(0).getAddr());
            bundle.putString("cid", getIntent().getStringExtra("cid"));
            show(SendingOrderActivity.class, bundle);
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {
            super.onEnd(toast, type);
            if (!TextUtils.isEmpty(toast)) GetToast.useString(cnt, toast);
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_info_sure);
        setTitle("订单确认");
        initView();
        payBalance();
    }

    private void initView() {

        ((TextView) findViewById(R.id.tv_public)).setOnClickListener(this);
        tv_tip_money = (TextView) findViewById(R.id.tv_tip_money);
        tv_tip = (TextView) findViewById(R.id.tv_tip);
        tv_tip.setOnClickListener(this);

        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_car_name = (TextView) findViewById(R.id.tv_car_name);
        tv_price = (TextView) findViewById(R.id.tv_price);
        tv_coupon = (TextView) findViewById(R.id.tv_coupon);
        tv_time.setText(getIntent().getStringExtra("time"));
        tv_car_name.setText(getIntent().getStringExtra("car_name"));
        tv_price.setText(getIntent().getStringExtra("money") + "元");
        if ("0".equals(getIntent().getStringExtra("coupom_money"))) {
            tv_coupon.setVisibility(View.GONE);
        } else {
            tv_coupon.setVisibility(View.VISIBLE);
            tv_coupon.setText("(已减免" + getIntent().getStringExtra("coupom_money") + "元)");
        }

        ((LinearLayout) findViewById(R.id.ll_send_pay)).setOnClickListener(this);
        ((LinearLayout) findViewById(R.id.ll_rec_pay)).setOnClickListener(this);
        tv_send_pay = (TextView) findViewById(R.id.tv_send_pay);
        tv_rec_pay = (TextView) findViewById(R.id.tv_rec_pay);
        ll_pay_method_container = (LinearLayout) findViewById(R.id.ll_pay_method_container);
        rl_cash_pay = (RelativeLayout) findViewById(R.id.rl_cash_pay);
        rl_acount_pay = (RelativeLayout) findViewById(R.id.rl_acount_pay);
        rl_acount_pay.setOnClickListener(this);
        rl_cash_pay.setOnClickListener(this);
        tv_acount_pay = (TextView) findViewById(R.id.tv_acount_pay);
        tv_cash_pay = (TextView) findViewById(R.id.tv_cash_pay);

        tv_balance_pay = (TextView) findViewById(R.id.tv_balance_pay);
        iv_alert = (ImageView) findViewById(R.id.iv_alert);

        money = getIntent().getStringExtra("money");

        lv = (MyListView) findViewById(R.id.lv);
        list = (List<AddrModel>) (getIntent().getSerializableExtra("addrs"));
        adapter = new AddrAdapter(cnt, list, false);
        lv.setAdapter(adapter);

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_tip://加小费
                showInputDialog();
                break;
            case R.id.ll_send_pay://发货时付款
                tv_send_pay.setSelected(true);
                tv_rec_pay.setSelected(false);
                ll_pay_method_container.setVisibility(View.VISIBLE);
                payttime = 1;
                break;
            case R.id.ll_rec_pay://收获是付货
                tv_send_pay.setSelected(false);
                tv_rec_pay.setSelected(true);
                ll_pay_method_container.setVisibility(View.VISIBLE);
                payttime = 2;
                break;
            case R.id.rl_cash_pay:// 现金支付
                tv_cash_pay.setSelected(true);
                tv_acount_pay.setSelected(false);
                paytype = 2;
                break;
            case R.id.rl_acount_pay://余额支付
                if (balanceEnough) {
                    tv_acount_pay.setSelected(true);
                    tv_cash_pay.setSelected(false);
                    paytype = 1;
                }
                break;
            case R.id.tv_public:
                if (payttime == 0) {
                    GetToast.useString(cnt, "请选择付费时间");
                    return;
                }
                if (paytype == 0) {
                    GetToast.useString(cnt, "请选择付费方式");
                    return;
                }
                getOrderCommit();
                break;
        }
    }

    /**
     * 加小费对话框
     */
    private void showInputDialog() {
        final InPutDialog dialog = new InPutDialog(cnt);
        dialog.setTitle("提示：优惠券不能抵扣小费费用");
        dialog.setInputKey("加小费");
        dialog.setHint("最高200元");
        dialog.setMaxValue(200);
        dialog.setMaxLength(3);
        dialog.setText(tip);
        dialog.setNo("取消");
        dialog.setYes("确认");
        dialog.setDialogOnClickListener(new InPutDialog.DialogOnClickListener() {
            @Override
            public void Yes(String temp) {
               /* if (TextUtils.isEmpty(temp)) {
                    GetToast.useString(cnt, "请输入小费金额");
                } else*/
                if ("0".equals(temp)) {
                    GetToast.useString(cnt, "金额不能为0");
                }else if (TextUtils.isEmpty(temp)){
                    GetToast.useString(cnt, "金额不能为空");
                }

                else if (!PublicMethod.formatDecimal(temp)) {
                    GetToast.useString(cnt, "数字格式错误");
                } else {
                    dialog.cancel();
                    tip = temp;
                    if (!temp.equals("")) {
                        tv_tip_money.setText("+" + temp + "元");
                    }
                    if (temp.equals("")) {
                        tv_tip_money.setText("");
                        tv_tip.setText("加小费");
                    }
                    tv_tip.setBackgroundResource(R.mipmap.tip_change);
                    tv_tip.setText("改小费");
                    if (balanceEnough) {
                        payBalance();
                    }
                }
            }
        });
        dialog.show();
    }

    /**
     * 提交订单
     */
    private void getOrderCommit() {

        Intent i = getIntent();
        Log.i("backmoney", i.getStringExtra("backmoney"));
        Log.i("ismove", i.getIntExtra("ismove", 0) + "");

        orderCommit.userid = AppContext.myPreferences.getUid();
        orderCommit.datetype = i.getIntExtra("datetype", 1);
        orderCommit.datetime = i.getStringExtra("datetime");
        orderCommit.account = i.getStringExtra("money");
        orderCommit.cid = i.getStringExtra("cid");
        orderCommit.saycontent = i.getStringExtra("saycontent");
        orderCommit.couponid = i.getStringExtra("couponid");
        orderCommit.backmoney = i.getStringExtra("backmoney");
        orderCommit.isbackmoney = i.getIntExtra("isbackmoney", 0);
        orderCommit.isbackorder = i.getIntExtra("isbackorder", 0);
        orderCommit.ismove = i.getIntExtra("ismove", 0);
        orderCommit.address = GetAddrAdd.addrListTojson(list);

        orderCommit.tip = tip;
        orderCommit.paytype = paytype;
        orderCommit.payttime = payttime;
        orderCommit.execute(cnt);
    }

    /**
     * 账户余额是否充足
     */
    private void payBalance() {
        double pay = PublicMethod.strToDouble(money) + PublicMethod.strToDouble(tip);
        new GetIsBalance(AppContext.myPreferences.getUid(), String.valueOf(pay), new AsyCallBack<Object>() {
            @Override
            public void onSuccess(String toast, int type, Object info) throws Exception {

                if ("2".equals(info)) {
                    tv_balance_pay.setText(getResources().getString(R.string.account_not_enough));
                    iv_alert.setVisibility(View.VISIBLE);
                    tv_cash_pay.setSelected(true);
                    tv_acount_pay.setSelected(false);
                    paytype = 2;
                    balanceEnough = false;
                } else if ("1".equals(info)) {
                    tv_balance_pay.setText(getResources().getString(R.string.account_enough));
                    iv_alert.setVisibility(View.GONE);
                    balanceEnough = true;
                } else {
                    tv_balance_pay.setText(getResources().getString(R.string.account_fail));
                    iv_alert.setVisibility(View.VISIBLE);
                }
            }
        }).execute(cnt);

    }
}
